package main

import (
	"fmt"
	"github.com/Shopify/sarama"
)

var(
	kafkaSender sarama.SyncProducer
	err error
)

func main(){
		//初始化生产者信息
		config := sarama.NewConfig()
		config.Producer.RequiredAcks =sarama.WaitForAll //发送完数据需要leader和follow都确认
		config.Producer.Partitioner  = sarama.NewRandomPartitioner //新选出一个partition
		config.Producer.Return.Successes = true // 成功交付的消息将在success channel返回

		kafkaSender,err =sarama.NewSyncProducer([]string{"127.0.0.1:9092"},config)
		if err != nil{
			panic(err)
		}
		// 构造一个消息
		msg := &sarama.ProducerMessage{}
		msg.Topic ="createorder"
		msg.Value = sarama.StringEncoder("this is a test log111")

		//发送消息
		pid,offset,err := kafkaSender.SendMessage(msg)

		if err != nil{
			fmt.Println("send msg failed, err:", err)
			return
		}
		fmt.Printf("pid:%v offset:%v\n", pid, offset)
}


